//
//  MeListCell.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/17.
//

#import "MeListCell.h"
#import "MeListBtnCell.h"
#import "MeBtnListInfoView.h"

@interface MeListCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UIImageView * bgView;

@property (nonatomic,strong) UIButton * hisBtn;
@property (nonatomic,strong) UIButton * giftBtn;

@property (nonatomic,strong) UIImageView    * line;
@property (nonatomic,strong) UICollectionView   * listView;
@property (nonatomic,strong) HomeTipsView * tipsView;
@property (nonatomic,strong) MeBtnListInfoView * btnListView;
@end

@implementation MeListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = UIColor.clearColor;
        self.contentView.backgroundColor = UIColor.clearColor;
        [self setSubViewLayout];
    }
    return self;
}
-(void)setSubViewLayout{
    
    [self addSubview:self.bgView];
        
    [self addSubview:self.hisBtn];
    [_hisBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(50 * FMScaleX);
        make.size.mas_offset(CGSizeMake(140 * FMScaleX, 70 * FMScaleX));
        make.right.equalTo(self.mas_centerX).offset(-6 * FMScaleX);
    }];
    [self addSubview:self.giftBtn];
    [_giftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.top.equalTo(self.hisBtn);
        make.left.equalTo(self.hisBtn.mas_right).offset(12 * FMScaleX);
    }];
    
    if (ISUPDATE) {
        
        [self addSubview:self.line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(@0);
            make.top.mas_offset(50 * FMScaleX);
            make.size.mas_offset(CGSizeMake(280 * FMScaleX, 8.48 * FMScaleX));
        }];
        
        [self addSubview:self.listView];
        [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(@0);
            make.top.equalTo(self.line.mas_bottom).offset(30 * FMScaleX);
            make.left.mas_offset(40 * FMScaleX);
            make.right.mas_offset(-40 * FMScaleX);
        }];
    }else{
        [self addSubview:self.line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(@0);
            make.top.equalTo(self.hisBtn.mas_bottom).offset(20 * FMScaleX);
            make.size.mas_offset(CGSizeMake(280 * FMScaleX, 8.48 * FMScaleX));
        }];
        
        [self addSubview:self.listView];
        [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(@0);
            make.top.equalTo(self.line.mas_bottom).offset(30 * FMScaleX);
            make.left.mas_offset(40 * FMScaleX);
            make.right.mas_offset(-40 * FMScaleX);
        }];
        
    }
    
    
   
    
    self.hisBtn.hidden = ISUPDATE;
    self.giftBtn.hidden = ISUPDATE;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 7;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    MeListBtnCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MeListBtnCell" forIndexPath:indexPath];
    [cell setIndex:indexPath.row];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(0* FMScaleX, 10* FMScaleX, 0* FMScaleX, 10* FMScaleX);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        id vc = [[NSClassFromString(@"MeBindInfoVC") alloc]init];
        [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    }
    if (indexPath.row == 1) {
        id vc = [[NSClassFromString(@"MeAddressVC") alloc]init];
        [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    }
    if (indexPath.row == 2) {
        id vc = [[NSClassFromString(@"ExchangeRecordVC") alloc]init];
        [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    }
    if (indexPath.row == 3) {
        id vc = [[NSClassFromString(@"AccountVC") alloc]init];
        [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    }
    if (indexPath.row == 4) {
        id vc = [[NSClassFromString(@"FeedBackVC") alloc]init];
        [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    }
    if (indexPath.row == 5) {
        id vc = [[NSClassFromString(@"AboutVC") alloc]init];
        [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    }
    if (indexPath.row == 6) {
        [self.tipsView startAnimationWithTitle:@"提示" info:@"是否退出当前账号?"];
    }
   
}

-(void)loginOutData{
    
    [SPNetworkingIns GET:PORT(@"auth-center/api/log/revokeToken") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLogin"];
        [(AppDelegate*) [UIApplication sharedApplication].delegate gotoApp];
        [(AppDelegate*) [UIApplication sharedApplication].delegate gotoLogin];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(void)hisBtnAction{
    
    id vc = [[NSClassFromString(@"HealthReportsVC") alloc]init];
    [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    
    
}

-(void)giftBtnAction{
    
    id vc = [[NSClassFromString(@"HealthGftKitVC") alloc]init];
    [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    
}

-(UIImageView *)bgView{
    
    if (!_bgView) {
        _bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 470 * FMScaleX)];
        _bgView.image = [UIImage imageNamed:@"Me_list_bg"];
    }
    return _bgView;
}

-(UIImageView *)line{
    
    if (!_line) {
        _line = [[UIImageView alloc]init];
        _line.image = [UIImage imageNamed:@"Me_list_line"];
    }
    return _line;
}

-(UICollectionView *)listView{

    if (!_listView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(50 * FMScaleX, 75 * FMScaleX);

        _listView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.backgroundColor = UIColor.clearColor;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        [_listView registerClass:[MeListBtnCell class] forCellWithReuseIdentifier:@"MeListBtnCell"];

    }
    return _listView;
}

-(HomeTipsView *)tipsView{
    
    if (!_tipsView) {
        _tipsView = [[HomeTipsView alloc]initWithFrame:[UIViewController currentViewController].view.bounds];
        @WeakObj(self)
        [_tipsView setEnterdBtnBlock:^{
            [[BCLRingManager shared] getBlcConnectWithCompletion:^(BOOL success) {
                @StrongObj(self)
                if (success) {
                    [[BCLRingManager shared] disconnect];
                    [self loginOutData];
                }else{
                    [self loginOutData];
                }
            }];
        }];
        [[UIViewController currentViewController].view addSubview:_tipsView];
    }
    return _tipsView;
}

-(MeBtnListInfoView *)btnListView{
    
    if (!_btnListView) {
        _btnListView = [[MeBtnListInfoView alloc]init];
    }
    return _btnListView;
}

-(UIButton *)hisBtn{
    
    if (!_hisBtn) {
        _hisBtn = [[UIButton alloc]init];
        [_hisBtn setBackgroundImage:[UIImage imageNamed:@"Me_reportbtn_icon"] forState:UIControlStateNormal];
        [_hisBtn addTarget:self action:@selector(hisBtnAction ) forControlEvents:UIControlEventTouchUpInside];
    }
    return _hisBtn;
}

-(UIButton *)giftBtn{
    
    if (!_giftBtn) {
        _giftBtn = [[UIButton alloc]init];
        [_giftBtn setBackgroundImage:[UIImage imageNamed:@"Me_giftbtn_icon"] forState:UIControlStateNormal];
        [_giftBtn addTarget:self action:@selector(giftBtnAction ) forControlEvents:UIControlEventTouchUpInside];
    }
    return _giftBtn;
}
@end
